package com.rabbitmq.demo.controller;

import com.rabbitmq.demo.pojo.Order;
import com.rabbitmq.demo.pojo.User;
import com.rabbitmq.demo.service.OrderService;
import com.rabbitmq.demo.util.RandomUtil;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.*;

@RestController
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Value("${order.expiration}")
    Long expiration;

    @RequestMapping("/order/create")
    public Long create(HttpSession session) {
        Map<String,Object> result = new HashMap<>();
        Long userId = (Long) session.getAttribute("userId");
        Order order = new Order();
        order.setUserId(userId);
        order.setOrderNo(RandomUtil.getRandomFileName());
        order.setState(1);
        order.setCreateTime(new Date());
        order = orderService.save(order);

        final MessageProperties messageProperties = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("utf-8")
                .setHeader("hello", "world")
                .build();

        final Message msg = MessageBuilder
                .withBody(order.getId().toString().getBytes())
                .andProperties(messageProperties)
                .build();

        rabbitTemplate.send("ex.order", "key.order", msg);

        return order.getId();
    }
    @RequestMapping("/order/{id}")
    public Map<String,Object> create(@PathVariable Long id,HttpSession session)  {
        Map<String,Object> result = new HashMap<>();
        Order order = orderService.getById(id);
        result.put("id",order.getId());
        result.put("orderNo",order.getOrderNo());
        result.put("createTime",order.getCreateTime().getTime());
        result.put("expiration",expiration);
        return result;
    }

    @RequestMapping("/order/pay/{id}")
    public boolean pay(@PathVariable Long id,HttpSession session)  {

        Order order = orderService.getById(id);
        //状态未变更，支付流程
        if(order.getState()==1){
            order.setState(2);
            orderService.update(order);
            return true;

        }

        return false;
    }

    @RequestMapping("/order/list")
    public List list(HttpSession session)  {
        List<String> list = new ArrayList<>();
        Long userId = (Long) session.getAttribute("userId");
        List<Order> order = orderService.getListByUserIdAndStateOrderByIdDesc(userId,3);
        for (Order order1 : order) {
            list.add(order1.getOrderNo());
        }

        return list;
    }
}
